<template>
  <!-- 抖音订单管理 -->
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
      :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="mb-[10px]" v-show="showSearch">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="订单号" prop="orderId">
              <el-input v-model.trim="queryParams.orderId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="用户ID" prop="userId">
              <el-input v-model.trim="queryParams.userId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="活动ID" prop="activityId">
              <el-input v-model.trim="queryParams.activityId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item v-show="tuonsein" label="商品ID" prop="goodsId">
              <el-input v-model.trim="queryParams.goodsId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item v-show="tuonsein" label="店铺ID" prop="shopId">
              <el-input v-model.trim="queryParams.shopId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item v-show="tuonsein" label="直推ID" prop="directUserId">
              <el-input v-model.trim="queryParams.directUserId" placeholder="请输入" clearable style="width: 200px" />
            </el-form-item>
            <el-form-item label="结算状态" prop="settleStatus">
              <el-select v-model="queryParams.settleStatus" placeholder="请选择" clearable style="width: 90px">
                <el-option label="已结算" :value="1" />
                <el-option label="未结算" :value="0" />
              </el-select>
            </el-form-item>
            <el-form-item label="付款时间" prop="payTimeList">
              <el-date-picker v-model="payTimeList" type="daterange" range-separator="-" start-placeholder="开始时间"
                end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :default-time="defaultTime"
                style="width: 230px" />
            </el-form-item>
            <el-form-item v-show="tuonsein" label="结算时间" prop="settleTimeList">
              <el-date-picker v-model="settleTimeList" type="daterange" range-separator="-" start-placeholder="开始时间"
                end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :default-time="defaultTime"
                style="width: 230px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
              <el-button @click="resetQuery" icon="Refresh">重置</el-button>
              <el-button link :icon="`${tuonsein ? 'ArrowUp' : 'ArrowDown'}`" @click="tuonsein = !tuonsein"></el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>
    <el-card shadow="hover">
      <!-- 功能按钮 -->
      <template #header>
        <el-row :gutter="30">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"
            :search="true"></right-toolbar>
        </el-row>
      </template>
      <!-- 表格 31 -->
      <el-table v-loading="loading" :data="tiktokList" max-height="550" border>
        <el-table-column label="订单号" prop="orderId" align="center" width="180" v-if="columns[0].visible" />
        <el-table-column label="用户" align="center" min-width="260" v-if="columns[1].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.userId">
                <el-col :span="7" class="cuionseti">ID：</el-col>
                <el-col :span="17">{{ scope.row.userId }}</el-col>
              </el-row>
              <el-row v-if="scope.row.directUserId">
                <el-col :span="7" class="cuionseti">上级ID：</el-col>
                <el-col :span="17">{{ scope.row.directUserId }}</el-col>
              </el-row>
              <el-row v-if="scope.row.agentId">
                <el-col :span="7" class="cuionseti">所属部门：</el-col>
                <el-col :span="17">{{ scope.row.agentId }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="推广者信息" align="center" width="180" v-if="columns[2].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <el-col :span="9" class="cuionseti">名称：</el-col>
                <el-col :span="15">{{ scope.row.authorName }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="9" class="cuionseti">抖音号：</el-col>
                <el-col :span="15">{{ scope.row.authorId }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="9" class="cuionseti">活动ID：</el-col>
                <el-col :span="15">{{ scope.row.activityId }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="商品信息" align="center" width="350" v-if="columns[3].visible">
          <template #default="scope">
            <el-row justify="space-between" align="middle">
              <el-image style="width: 80px; height: 80px"
                :src="scope.row.goodsCover "
                :preview-src-list="[scope.row.goodsCover]" fit="contain" :preview-teleported="true">
                <template #error>
                  <div></div>
                </template>
              </el-image>
              <el-col :span="17">
                <div class="lioneuo">
                  <el-row>
                    <el-tooltip :content="scope.row.goodsName" placement="top">
                      <el-col class="mu-row goods_text">{{ scope.row.goodsName }}</el-col>
                    </el-tooltip>
                  </el-row>
                  <el-row>
                    <span class="cuionseti">商品ID：</span>
                    <span>{{ scope.row.goodsId }}</span>
                  </el-row>
                  <el-row>
                    <el-col :span="7" class="cuionseti">商品数量：</el-col>
                    <el-col :span="16">{{ scope.row.goodsNum }}</el-col>
                  </el-row>
                </div>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="店铺信息" align="center" width="200" v-if="columns[4].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <el-col :span="6" class="cuionseti">名称：</el-col>
                <el-col :span="16">{{ scope.row.shopName }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="6" class="cuionseti">ID：</el-col>
                <el-col :span="16">{{ scope.row.shopId }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="130" v-if="columns[5].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <el-col :span="9" class="cuionseti">订单：</el-col>
                <el-col :span="13">{{ statusType(scope.row.status) }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="9" class="cuionseti">结算：</el-col>
                <el-col :span="13">{{ settleStatusType(scope.row.settleStatus) }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="团长" align="center" width="200" v-if="columns[6].visible">
          <template #default="scope">
            <div class="lioneuo">
              <!-- <el-row>
                <el-col :span="7" class="cuionseti">ID：</el-col>
                <el-col :span="17">{{ scope.row.accountId }}</el-col>
              </el-row> -->
              <el-row>
                <el-col :span="10" class="cuionseti">名称：</el-col>
                <el-col :span="14">{{ scope.row.account }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="10" class="cuionseti">带货载体：</el-col>
                <el-col :span="14">{{ scope.row.mediaTypeName }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="返佣" align="center" width="160" v-if="columns[7].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.rebate !== '0'">
                <el-col :span="14" class="cuionseti">返佣金额：</el-col>
                <el-col :span="10">￥{{ scope.row.rebate }}</el-col>
              </el-row>
              <el-row v-if="scope.row.directRebate !== '0'">
                <el-col :span="14" class="cuionseti">上级返佣：</el-col>
                <el-col :span="10">￥{{ scope.row.directRebate }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="服务/佣金" align="center" width="160" v-if="columns[8].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row>
                <el-col :span="12" class="cuionseti">服务费率：</el-col>
                <el-col :span="12">{{ scope.row.serviceRate }}%</el-col>
              </el-row>
              <el-row>
                <el-col :span="12" class="cuionseti">服务费：</el-col>
                <el-col :span="12">￥{{ scope.row.payServiceFee }}</el-col>
              </el-row>
              <el-row>
                <el-col :span="12" class="cuionseti">佣金率：</el-col>
                <el-col :span="12">{{ scope.row.commissionRate }}%</el-col>
              </el-row>
              <el-row>
                <el-col :span="12" class="cuionseti">佣金：</el-col>
                <el-col :span="12">￥{{ scope.row.commissionFee }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="付款/结算" align="center" width="160" v-if="columns[9].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.flowPrice !== '0'">
                <el-col :span="14" class="cuionseti">付款金额：</el-col>
                <el-col :span="10">￥{{ scope.row.flowPrice }}</el-col>
              </el-row>
              <el-row v-if="scope.row.settledAmount !== '0'">
                <el-col :span="14" class="cuionseti">结算金额：</el-col>
                <el-col :span="10">￥{{ scope.row.settledAmount }}</el-col>
              </el-row>
              <el-row v-if="scope.row.settleServiceFee !== '0'">
                <el-col :span="14" class="cuionseti">结算服务费：</el-col>
                <el-col :span="10">￥{{ scope.row.settleServiceFee }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="收货/结算时间" align="center" width="200" v-if="columns[10].visible">
          <template #default="scope">
            <div class="lioneuo">
              <el-row v-if="scope.row.confirmTime">
                <el-col :span="6" class="cuionseti">收货：</el-col>
                <el-col :span="18">{{ scope.row.confirmTime.slice(0, -3) }}</el-col>
              </el-row>
              <el-row v-if="scope.row.settleTime">
                <el-col :span="6" class="cuionseti">结算：</el-col>
                <el-col :span="18">{{ scope.row.settleTime.slice(0, -3) }}</el-col>
              </el-row>
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="付款时间" prop="payTime" align="center" width="140" v-if="columns[11].visible">
            <template #default="scope">{{ scope.row.payTime.slice(0, -3) }}</template>
          </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="dinseuom">
        <!-- 订单统计 -->
        <statistics :statistics="statisticsObj"></statistics>
        <!-- 分页 -->
        <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" @pagination="getList"
          v-model:limit="queryParams.pageSize" />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts" name="TiktokOrder">
import { listTiktokOrder, statisticsTiktokOrder } from '@/api/operates/tiktokOrder/index';
import { TiktokOrderQuery, TiktokOrderVO, TiktokOrderStatistics } from '@/api/operates/tiktokOrder/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const loading = ref(true)
const showSearch = ref(true)
const tuonsein = ref(false)
const total = ref(0)
// 付款时间
const payTimeList = ref<any>([])
// 结算时间
const settleTimeList = ref<any>([])
const queryFormRef = ref<ElFormInstance>();
// 表格数据
const tiktokList = ref<TiktokOrderVO[]>([]);
const statisticsObj = ref<TiktokOrderStatistics>();
// 默认时间
const defaultTime = ref<[Date, Date]>([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59),
])
// 查询表单
const queryParams = ref<TiktokOrderQuery>({
  pageNum: 1,
  pageSize: 10,
});

const columns = ref<FieldOption[]>([
  { key: 0, label: `订单号`, visible: true, children: [] },
  { key: 1, label: `用户`, visible: true, children: [] },
  { key: 2, label: `推广者信息`, visible: true, children: [] },
  { key: 3, label: `商品信息`, visible: true, children: [] },
  { key: 4, label: `店铺信息`, visible: true, children: [] },
  { key: 5, label: `状态`, visible: true, children: [] },
  { key: 6, label: `团长`, visible: true, children: [] },
  { key: 7, label: `返佣`, visible: true, children: [] },
  { key: 8, label: `服务/佣金`, visible: true, children: [] },
  { key: 9, label: `付款/结算`, visible: true, children: [] },
  { key: 10, label: `收货/结算时间`, visible: true, children: [] },
  { key: 11, label: `付款时间`, visible: true, children: [] },
])

/**
 * 查询商品列表
 */
const getList = async () => {
  loading.value = true
  const query = { ...queryParams.value }
  if (payTimeList.value.length) {
    query.payStartTime = payTimeList.value[0]
    query.payEndTime = payTimeList.value[1]
  }
  if (settleTimeList.value.length) {
    query.settleStartTime = settleTimeList.value[0]
    query.settleEndTime = settleTimeList.value[1]
  }
  const res = await listTiktokOrder(query);
  const { data } = await statisticsTiktokOrder(query);
  tiktokList.value = res.data.list;
  total.value = res.data.total;
  statisticsObj.value = data;
  loading.value = false;
}

/**
 * 搜索按钮操作
 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/**
 * 重置搜索
 */
const resetQuery = () => {
  payTimeList.value = [];
  settleTimeList.value = [];
  queryFormRef.value?.resetFields();
  handleQuery();
}

// 订单状态 1付款订单   4失效订单 5收货订单 2结算订单
const statusType = (val: number) => {
  if (val === 1) {
    return '付款订单'
  } else if (val === 2) {
    return '结算订单'
  } else if (val === 4) {
    return '失效订单'
  } else if (val === 5) {
    return '收货订单'
  }
};
// 结算状态，-1=已失效，0 = 待结算，1 = 已结算
const settleStatusType = (val: number) => {
  if (val === -1) {
    return '已失效'
  } else if (val === 0) {
    return '待结算'
  } else if (val === 1) {
    return '已结算'
  }
};

onMounted(() => {
  getList();
});
</script>

<style scoped>
.statis {
  font-size: 16px;
}

.statis_num {
  color: #f00404;
}

/* 单行省略 */
.sin-line {
  /* 第一步：让文字强制一行显示 */
  white-space: nowrap;
  /* 第二步：溢出隐藏 */
  overflow: hidden;
  /* 第三步：溢出的内容省略号显示 */
  text-overflow: ellipsis;
}

/* 多行省略 */
.mu-row {
  /* 允许单词内换行 */
  word-break: break-all;
  overflow: hidden;
  text-overflow: ellipsis;
  /* 开启盒子的弹性布局 */
  display: -webkit-box;
  /* 控制第几行出现省略号 */
  -webkit-line-clamp: 2;
  /* 设置子元素从上向下垂直排列 */
  -webkit-box-orient: vertical;
}

.goods_text {
  font-weight: 600;
}
</style>